package com.example.service.impl;

import com.example.entity.BuyList;
import com.example.entity.Result;
import com.example.mapper.BuyListMapper;
import com.example.mapper.ProductMapper;
import com.example.page.Page;
import com.example.service.BuyListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@Service
public class BuyListServiceImpl implements BuyListService {
    @Autowired
    private BuyListMapper buyListMapper;
    @Autowired
    private ProductMapper productMapper;
    /**
     * 添加采购单的业务方法
     * @param buyList
     * @return
     */
    @Override
    public Result saveBuyList(BuyList buyList) {
        // 持久化采购单
        if (buyListMapper.insertBuyList(buyList) <= 0) {
            return Result.err(Result.CODE_ERR_SYS, "系统忙，请稍后重试！");
        }
        return Result.ok("采购单添加成功！");
    }

    /**
     * 根据条件查询采购单并分页的业务方法
     * @param buyList
     * @param page
     */
    @Override
    public void queryBuyListByConditionForPagination(BuyList buyList, Page page) {
        // 根据条件，查询采购单的记录条数
        int buyListCount = buyListMapper.selectBuyListCountByCondition(buyList);

        // 根据条件，查询采购单并分页
        List<BuyList> buyLists = buyListMapper.selectBuyListByConditionForPagination(buyList, page);

        // 封装参数page对象
        page.setTotalNum(buyListCount);
        page.setResultList(buyLists);
    }

    /**
     * 根据id修改采购单的业务方法
     *
     * @param buyList
     * @return
     */
    @Override
    public Result modifyBuyListById(BuyList buyList) {
        // 修改采购单
        if (buyListMapper.updateBuyListById(buyList) <= 0) {
            return Result.err(Result.CODE_ERR_SYS, "系统忙，请稍后重试！");
        }
        return Result.ok("修改成功！");
    }
}
